import {Component, OnInit} from '@angular/core';
import {Page} from '../../../public/util/page';
import {UserService} from '../user.service';
import {Enums, UserStates, UserTypes, YesOrNo} from '../../../public/setting/enums';
import {SettingUrl} from '../../../public/setting/setting_url';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {Setting} from '../../../public/setting/setting';
import {MainService} from '../../../public/service/main.service';

@Component({
  selector: 'app-user-list',
  templateUrl: './user-list.component.html',
  styleUrls: ['./user-list.component.scss']
})
export class UserListComponent implements OnInit {
  public searchParams: any = {};//搜索参数
  public userList: Page = new Page(); //供应商
  public _loading: boolean = false;
  public routerLinks = SettingUrl.ROUTERLINK;//路由
  public userStateCode = Enums.userState;       // 状态枚举码
  public isResetPwdVisible: boolean = false;//重置密码窗口是否显示
  public validateForm: FormGroup;         //表单
  public showList: boolean = true;    //是否显示列表
  public isOkLoading: boolean = false;    //确定按钮加载状态
  public userCode: string;
  public userType = Enums.userType;       // 是否是领导人
  public userTypes = UserTypes;       // 用户类型
  public userStates = UserStates;       // 用户的状态
  public userTypeList: Array<any> = [];       // 用户类型枚举
  public userStateList: Array<any> = [];       // 用户状态枚举
  public stars: Array<any> = [];       // 星级
  pageSize = [10, 20, 30, 50, 100];

  constructor(private fb: FormBuilder, private userService: UserService, private mainService: MainService) {
    this.validateForm = this.fb.group({
      password: [null, Validators.compose([Validators.required])], // 密码
    });
    this.userTypeList = this.mainService.getEnumDataList(Enums.userType);
    this.userStateList = this.mainService.getEnumDataList(Enums.userState);
    this.userTypeList.unshift({key: '', val: '全部'});
    this.userStateList.unshift({key: '', val: '全部'});
    for (let i = 1; i < 8; i++) {
      this.stars.push({key: i, val: i + '星级'});
    }
    this.stars.unshift({key: '', val: '全部'});
  }

  ngOnInit() {
    this.queryUserList();
  }

  /**
   * 查询user列表
   * @param curPage 当前页
   */
  queryUserList(curPage?: number, pageSize?: number) {
    this._loading = true;
    if (curPage) this.userList.curPage = curPage;//当有页码时，查询该页数据
    if (pageSize) this.userList.pageSize = pageSize;//当有页码时，查询该页数据
    this.userList.params = {
      curPage: this.userList.curPage, //目标页码
      pageSize: this.userList.pageSize, //每页条数
      account: this.searchParams.account, //搜索条件-账户
      star: this.searchParams.star, //搜索条件-星级
      state: this.searchParams.state, //搜索条件-状态
      leader: this.searchParams.userType, //搜索条件-用户类型
    };
    this.userService.getUserList(this.userList.params).then((res: Page) => {
      this._loading = false;
      this.userList = res;
    }).catch(err => {
      this._loading = false;
    });
  }

  activate(event) {
    this.showList = false;
  }

  onDeactivate(event) {
    this.showList = true;
    if (event.ifRefreshList) this.queryUserList();//如果子页面有修改则返回时刷新列表
  }

  /**
   * 重置密码
   */
  resetPassword(userCode) {
    this.userCode = userCode;
    this.isResetPwdVisible = true;
  }

  /**
   * 确定按钮
   */
  updateUserPwd() {
    //进行脏检查，提示未填的必填字段
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsDirty();
      this.validateForm.controls[key].updateValueAndValidity();
    }
    if (this.validateForm.invalid) return;
    this.isOkLoading = true;
    // 表单验证通过之后提交
    this.userService.updateUserPwd(this.userCode, this.validateForm.value.password).then(data => {
      this.isOkLoading = false;
      this.handleCancel();
    }).catch(res => {
      this.isOkLoading = false;
    });
  }

  loginUserAccount(account) {
    this.userService.signInUser(account).then((data: any) => {
      if (data) window.open(data);
    });
  }

  /**
   * 修改用户状态
   */
  updateState(userCode,state) {
    this.userService.modify(userCode, state).then(data => {
      this.queryUserList();
    });
  }

  /**
   * 取消按钮
   */
  handleCancel() {
    this.validateForm.reset();
    this.isResetPwdVisible = false;
  }

  /**
   * 重置搜索
   */
  resetSearch() {
    this.searchParams = {};
    this.queryUserList(1);
  }

  getFormControl(name) {
    return this.validateForm.controls[name];
  }
}
